package com.huang.foundation.crawler;

import com.alibaba.fastjson.JSON;
import com.huang.foundation.FoundationServiceApplication;
import com.huang.foundation.entity.po.ZoningCodePO;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author HuangShen
 * @Description 区划数据爬取
 * @create 2021-09-19 8:06
 */
public class ZoningCodePageProcessor implements PageProcessor {


    // 爬取内容
    private String type;
    private String css;


    public ZoningCodePageProcessor(String type, String css) {
        this.type = type;
        this.css = css;
    }


    // 部分一：抓取网站的相关配置，包括编码、抓取间隔、重试次数等
    private final Site site = Site.me()
            .setRetryTimes(5)
            .setSleepTime(1000)
            .setCharset("gb2312")
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36 Edg/93.0.961.52");


    @Override
    // process是定制爬虫逻辑的核心接口，在这里编写抽取逻辑
    public void process(Page page) {
        // 部分二：定义如何抽取页面信息，并保存下来
        Html html = page.getHtml();
        ArrayList<ZoningCodePO> zoningCodePOS;
        switch (type) {
            case "province":
                zoningCodePOS = new ArrayList<>();
                List<String> provinces = html.css(css).all();
                provinces.forEach(province -> {
                    ZoningCodePO zoningCodePO = new ZoningCodePO();
                    Html provinceDoc = new Html(province);
                    String name = provinceDoc.getDocument().text();
                    String id = provinceDoc.css("a", "href").toString().replace(".html", "");
                    zoningCodePO.setId(Long.valueOf(id));
                    zoningCodePO.setName(name);
                    zoningCodePO.setValue(Long.valueOf(id));
                    zoningCodePO.setDictCode("Province");
                    zoningCodePOS.add(zoningCodePO);

                });
                page.putField("data", zoningCodePOS);
                break;
            case "city":
                List<String> citys = html.css(css).all();
                zoningCodePOS = new ArrayList<>();
                citys.forEach(city -> {
                    Html cityDoc = new Html(city);
                    String id = Html.create(cityDoc.css("a:nth-child(1)").get()).getDocument().text();
                    String name = Html.create(cityDoc.css("a:nth-child(2)").get()).getDocument().text();

                    ZoningCodePO zoningCodePO = new ZoningCodePO();
                    zoningCodePO.setParentId(Long.valueOf(id.substring(0, 2)));
                    zoningCodePO.setId(Long.valueOf(id));
                    zoningCodePO.setName(name);
                    zoningCodePO.setValue(Long.valueOf(id));
                    zoningCodePO.setDictCode("City");

                    zoningCodePOS.add(zoningCodePO);

                });
                page.putField("data", zoningCodePOS);
                break;
            case "county":
                List<String> countys = html.css(css).all();
                zoningCodePOS = new ArrayList<>();
                countys.forEach(city -> {
                    Html cityDoc = new Html(city);
                    if (cityDoc.css("a:nth-child(1)").get() == null) return;
                    String id = Html.create(cityDoc.css("a:nth-child(1)").get()).getDocument().text();
                    String name = Html.create(cityDoc.css("a:nth-child(2)").get()).getDocument().text();
                    ZoningCodePO zoningCodePO = new ZoningCodePO();
                    zoningCodePO.setParentId(Long.valueOf(id.substring(0, 4)+"00000000"));
                    zoningCodePO.setId(Long.valueOf(id));
                    zoningCodePO.setName(name);
                    zoningCodePO.setValue(Long.valueOf(id));
                    zoningCodePO.setDictCode("County");
                    zoningCodePOS.add(zoningCodePO);
                    if (name.contains("�")) FoundationServiceApplication.err_urls.put(id,page.getUrl().toString());
                });
                page.putField("data", zoningCodePOS);
                break;
            case "town":
                List<String> towns = html.css(css).all();
                zoningCodePOS = new ArrayList<>();
                towns.forEach(city -> {
                    Html cityDoc = new Html(city);
                    if (cityDoc.css("a:nth-child(1)").get() == null) return;
                    String id = Html.create(cityDoc.css("a:nth-child(1)").get()).getDocument().text();
                    String name = Html.create(cityDoc.css("a:nth-child(2)").get()).getDocument().text();
                    ZoningCodePO zoningCodePO = new ZoningCodePO();
                    zoningCodePO.setParentId(Long.valueOf(id.substring(0, 6)+"000000"));
                    zoningCodePO.setId(Long.valueOf(id));
                    zoningCodePO.setName(name);
                    zoningCodePO.setValue(Long.valueOf(id));
                    zoningCodePO.setDictCode("Town");
                    zoningCodePOS.add(zoningCodePO);
                    if (name.contains("�")) FoundationServiceApplication.err_urls.put(id,page.getUrl().toString());
                });
                page.putField("data", zoningCodePOS);
                break;
            case "village":
                List<String> villages = html.css(css).all();
                zoningCodePOS = new ArrayList<>();
                villages.forEach(city -> {
                    Html cityDoc = new Html(city);
                    String[] text = cityDoc.getDocument().text().split(" ");
                    String id = text[0];
                    String classification= text[1];
                    HashMap<String, String> stringStringHashMap = new HashMap<>();
                    stringStringHashMap.put("classification",classification);
                    String name = text[2];
                    ZoningCodePO zoningCodePO = new ZoningCodePO();
                    zoningCodePO.setParentId(Long.valueOf(id.substring(0, 9)+"000"));
                    zoningCodePO.setId(Long.valueOf(id));
                    zoningCodePO.setName(name);
                    zoningCodePO.setValue(Long.valueOf(id));
                    zoningCodePO.setExpansion(JSON.toJSONString(stringStringHashMap));
                    zoningCodePO.setDictCode("Village");
                    zoningCodePOS.add(zoningCodePO);
                    if (name.contains("�")) FoundationServiceApplication.err_urls.put(id,page.getUrl().toString());
                });
                page.putField("data", zoningCodePOS);
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + type);
        }


    }

    @Override
    public Site getSite() {

        return site;
    }

}
